import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import {
  deleteLeaveWord,
  exportLeaveWord,
  getIndexCharts,
  getLeaveWordList,
} from "../../service/modules/leaveWord";

interface leaveWordState {
  chartData: any;
  leaveWordList: any[];
  total: number;
}
const initialState: leaveWordState = {
  chartData: {},
  leaveWordList: [],
  total: 0,
};

export const fetchGetIndexChartAction = createAsyncThunk(
  "index/chart",
  async () => {
    const res = await getIndexCharts();
    console.log(res);
    return res.data;
  }
);
/**
 * @获取留言列表
 */
export const fetchLeaveWordListAction = createAsyncThunk(
  "leaveWord/list",
  async (query: { pageNum?: number; pageSize?: number }) => {
    const res = await getLeaveWordList(query);
    // console.log(res);
    return res;
  }
);

/**
 * @删除留言
 */
export const fetchDeleteLeaveWordAction = createAsyncThunk(
  "leaveWord/delete",
  async (id: number) => {
    const res = await deleteLeaveWord(id);
    return res;
  }
);

/**
 * @导出留言
 */
export const fetchExportLeaveWordAction = createAsyncThunk(
  "leaveWoed/export",
  async (query: { pageNum: number; pageSize: number }) => {
    const res = await exportLeaveWord(query);
    // console.log(res);
    return res;
  }
);
const leaveWordSlice = createSlice({
  name: "leaveWord",
  initialState,
  reducers: {},
  extraReducers: (builder) => {
    builder
      .addCase(fetchGetIndexChartAction.fulfilled, (state, action) => {
        state.chartData = action.payload;
      })
      .addCase(fetchLeaveWordListAction.fulfilled, (state, action) => {
        state.leaveWordList = action.payload.rows;
        state.total = action.payload.total;
      });
  },
});
export default leaveWordSlice.reducer;
